<?php

namespace App\Rules\Auth;

use App\Models\Account;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class Validator
{
    /**
     * 登录密码验证
     *
     * @param $attribute
     * @param $value
     * @param $parameters
     * @param $validator
     *
     * @return bool
     */
    public function loginPassword($attribute, $value, $parameters, $validator)
    {
        return Hash::check($value, Auth::user()->password);
    }

    /**
     * 交易密码验证
     *
     * @param $attribute
     * @param $value
     * @param $parameters
     * @param $validator
     *
     * @return bool
     */
    public function payPassword($attribute, $value, $parameters, $validator)
    {
        return Hash::check($value, Auth::user()->pay_password);
    }
    /**
     * 交易密码验证
     *
     * @param $attribute
     * @param $value
     * @param $parameters
     * @param $validator
     *
     * @return bool
     */
    public function checknum($attribute, $value, $parameters, $validator)
    {
        $user = Account::where('user_id', Auth::id())->first();

        if ($value > $user->available ||  !is_int($value / 10)) {
            return false;
        } else {
            return true;
        }
    }
    /**
     * 交易密码验证
     *
     * @param $attribute
     * @param $value
     * @param $parameters
     * @param $validator
     *
     * @return bool
     */
    public function password($attribute, $value, $parameters, $validator)
    {
        return Hash::check($value, Auth::user()->password);
    }

    /**
     * 邮箱或手机号与账号是否匹配
     * @param $attribute
     * @param $value
     * @param $parameters
     * @param $validator
     * @return bool
     */
    public function emailOrMobileExists($attribute, $value, $parameters, $validator)
    {
        /** @var User $user */
        $user = User::where('username', $parameters[0])->first();
        if ($value == $user->mobile || $value == $user->email) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 交易密码必须6位数字
     * @param $attribute
     * @param $value
     * @param $parameters
     * @param $validator
     * @return bool
     */
    public function payPasswordFormat($attribute, $value, $parameters, $validator)
    {
        if (6 === strlen($value) && is_numeric($value)) {
            return true;
        } else {
            return false;
        }
    }

    public function usernameExists($attribute, $value, $parameters, $validator)
    {
        return User::where('username', $value)->exists();
    }
}
